package com.eseo.munchkinarbiter;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

public class PlayerEventListener extends GameChangeListener implements OnClickListener {
	private GameData gameData;
	private Button lvlPlusButton;
	private Button lvlMinusButton;
	private TextView lvlLabel;
	private Button gearPlusButton;
	private Button gearMinusButton;
	private TextView gearLabel;
	private CheckBox isWarriorCheck;
	private TextView strengthLabel;
	private Button previousPlayer;
	private Button nextPlayer;
	private TextView playerLabel;
	
	public PlayerEventListener(View rootView, GameData gameData) {
		this.gameData = gameData;
		
		lvlPlusButton = (Button) rootView.findViewById(R.id.lvlmore);
		lvlMinusButton = (Button) rootView.findViewById(R.id.lvlminus);
		lvlLabel = (TextView) rootView.findViewById(R.id.lvllabel);
		gearPlusButton = (Button) rootView.findViewById(R.id.gearmore);
		gearMinusButton = (Button) rootView.findViewById(R.id.gearminus);
		gearLabel = (TextView) rootView.findViewById(R.id.gearlabel);
		isWarriorCheck = (CheckBox) rootView.findViewById(R.id.warrior);
		strengthLabel = (TextView) rootView.findViewById(R.id.strengthlabel);
		previousPlayer = (Button) rootView.findViewById(R.id.playerprevious);
		nextPlayer = (Button) rootView.findViewById(R.id.playernext);
		playerLabel = (TextView) rootView.findViewById(R.id.playerlabel);

		lvlPlusButton.setOnClickListener(this);
		lvlMinusButton.setOnClickListener(this);
		gearPlusButton.setOnClickListener(this);
		gearMinusButton.setOnClickListener(this);
		isWarriorCheck.setOnClickListener(this);
		previousPlayer.setOnClickListener(this);
		nextPlayer.setOnClickListener(this);
	}
	
	public void onWarriorStateChanged(GameData gameData, PlayerStats player) {
		super.onWarriorStateChanged(gameData, player);
		if(player == gameData.getCurrentPlayer())
			isWarriorCheck.setChecked(gameData.getCurrentPlayer().isWarrior());
	}

	public void onPlayerNameChanged(GameData gameData, PlayerStats player) {
		super.onPlayerNameChanged(gameData, player);
		if(player == gameData.getCurrentPlayer())
			playerLabel.setText(gameData.getCurrentPlayer().getName());
	}
	
	public void onPlayerLevelChanged(GameData gameData, PlayerStats player) {
		super.onPlayerLevelChanged(gameData, player);
		if(player == gameData.getCurrentPlayer())
			lvlLabel.setText(Integer.toString(gameData.getCurrentPlayer().getLevel()));
	}
	public void onPlayerGearChanged(GameData gameData, PlayerStats player) {
		super.onPlayerGearChanged(gameData, player);
		if(player == gameData.getCurrentPlayer())
			gearLabel.setText(Integer.toString(gameData.getCurrentPlayer().getGear()));
	}
	
	public void onPlayerStrengthChanged(GameData gameData, PlayerStats player) {
		super.onPlayerStrengthChanged(gameData, player);
		if(player == gameData.getCurrentPlayer())
			strengthLabel.setText(Integer.toString(gameData.getCurrentPlayer().getStrength()));
	}
	
	public void onClick(View v) {
		if(v == lvlPlusButton) {
			gameData.getCurrentPlayer().incLevel();
		} else if(v == lvlMinusButton) {
			gameData.getCurrentPlayer().decLevel();
		} else if(v == gearPlusButton) {
			gameData.getCurrentPlayer().incGear();
		} else if(v == gearMinusButton) {
			gameData.getCurrentPlayer().decGear();
		} else if(v == isWarriorCheck) {
			gameData.getCurrentPlayer().setWarrior(isWarriorCheck.isChecked());
		} else if(v == previousPlayer) {
			gameData.previousPlayer();
		} else if(v == nextPlayer) {
			gameData.nextPlayer();
		}
	}
}
